package com.nf.xiaomi.service.impl;

import com.nf.xiaomi.dao.OrderDetailDao;
import com.nf.xiaomi.dao.ProductDao;
import com.nf.xiaomi.dao.impl.OrderDetailDaoImpl;
import com.nf.xiaomi.dao.impl.ProductDaoImpl;
import com.nf.xiaomi.entity.OrderEntity;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.model.AttributeModel;
import com.nf.xiaomi.service.OrderDetailService;
import com.nf.xiaomi.util.JsonUtils;

import java.util.List;

public class OrderDetailServiceImpl implements OrderDetailService {
    OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
    ProductDao productDao = new ProductDaoImpl();
    @Override
    public List<OrderEntity> getAll() {
        List<OrderEntity> orderDaoAll = orderDetailDao.getAll();
        getOrderAdditionalData(orderDaoAll);
        return orderDaoAll;
    }

    private void getOrderAdditionalData(List<OrderEntity> orderDaoAll) {
        Integer[] ids = getDetailIds(orderDaoAll);
        List<ProductEntity> attributeList = productDao.getProductAndAttributeList(ids);
        for (OrderEntity orderEntity : orderDaoAll) {
            for (ProductEntity productEntity : attributeList) {
                if(productEntity.getProductDetailID()==orderEntity.getProductDetailID()) {
                    AttributeModel model = JsonUtils.readValue(productEntity.getAttributeList(),
                            AttributeModel.class);
                    orderEntity.setAttributeList(model.getMemory()+" "+model.getColor());
                    orderEntity.setProductName(productEntity.getProductName());
                    orderEntity.setProductDetailPrice(productEntity.getProductDetailPrice());
                    orderEntity.setProductPictureList(productEntity.getProductPictureList());
                }
            }
        }
    }

    @Override
    public List<OrderEntity> getAll(Integer orderState) {
        List<OrderEntity> orderDaoAll = orderDetailDao.getAll(orderState);
        getOrderAdditionalData(orderDaoAll);
        return orderDaoAll;
    }

    public List<OrderEntity> getAll(Long account) {
        List<OrderEntity> orderDaoAll = orderDetailDao.getAll(account);
        getOrderAdditionalData(orderDaoAll);
        return orderDaoAll;
    }

    public List<OrderEntity> confirmOrder(Long account, Integer orderID) {
        List<OrderEntity> orderDaoAll = orderDetailDao.confirmOrder(account, orderID);
        getOrderAdditionalData(orderDaoAll);
        return orderDaoAll;
    }

    @Override
    public Integer insert(OrderEntity order) {
        Integer insert = 0;
        if (order.getProductDetailID()==null){
            for (Integer orderID : order.getProductDetailIDList()) {
                order.setProductDetailID(orderID);
                insert = orderDetailDao.insert(order);
            }
        }
        else {
            insert = orderDetailDao.insert(order);
        }
        return insert;
    }

    public Integer deleteByID(Integer orderID) {
        return orderDetailDao.deleteByID(orderID);
    }
}
